package com.example.caorl.jdbcaop.util;

import java.sql.SQLException;

public class TransactionManager {
//    private ConnectionUtils connectionUtils;

    private JDBCTemplate jdbcTemplate;

    public void setJdbcTemplate(JDBCTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void beginTransaction(){
        //开启事务
        try {
            System.out.println("aop前置------------");
            jdbcTemplate.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void commit(){
        //提交事务
        try {
            System.out.println("aop后置------------");
            jdbcTemplate.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void rollback(){
        //回滚事务
        try {
            System.out.println("aop异常事务回滚------------");
            jdbcTemplate.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void release(){
        //释放链接
        try {
            System.out.println("aop后置释放连接------------");
            jdbcTemplate.getConnection().close();//把conn还回连接池中
            jdbcTemplate.removeConnection();//将conn和线程解绑
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

